package main

import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup
var total int
	//互斥锁
var	lock sync.Mutex

func sub()  {
	defer wg.Done()
	for i:=0;i<100000;i++ {
		lock.Lock()
		total = total-1
		lock.Unlock()
	}
}
func add() {
	defer wg.Done()
	for i:=0;i<100000;i++{
		lock.Lock()
		total = total+1;
		lock.Unlock()
	}
}
func main()  {

	wg.Add(2) //因为启动两个子协程，故计数为2
	go add()
	go sub()
	wg.Wait()
	fmt.Println(total)
}
